package util;

import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * hashmap
 * ConcurrentHashMap
 * treemap
 *
 * @author Khai Loon
 * @version 2017/4/6 0006 下午 2:45
 */
public class Map_ {


    public static void main(String[] args) {
        System.out.println(new Object().hashCode());
        System.out.println(new Object().hashCode());
        System.out.println(new Object().hashCode());
    }

    @Test
    public void pre() {
        TreeMap<Integer, String> lMap = new TreeMap<>();
        lMap.put(1, "a");
        lMap.put(2, "a");
        lMap.put(3, "a");

        Set<Map.Entry<Integer, String>> entry = lMap.entrySet();
        Iterator iterator = entry.iterator();
        while (iterator.hasNext()) {
            System.out.println(((Map.Entry) iterator.next()).getKey());
        }
    }


    @Test
    public void count() {
        Map<Integer, String> lMap = new ConcurrentHashMap<>();
        lMap.put(1, "a");
        lMap.put(2, "a");
        lMap.put(3, "a");


        int size = lMap.size();
    }

    @Test
    public void concurrentHashMap_computeIfAbsent() {
        ConcurrentHashMap<String, String> hashMap = new ConcurrentHashMap<>();
        hashMap.computeIfAbsent("key1", (k) -> k + 1);

        //已存在key 返回 old val
        hashMap.computeIfAbsent("key1", (k) -> {
            return k + 1;
        });

    }

    @Test
    public void concurrentHashMap_computeIfPresent() {
        ConcurrentHashMap<String, String> hashMap = new ConcurrentHashMap<>();

        hashMap.put("key1", "1");

        //没key 直接返回
        hashMap.computeIfPresent("key1", (k, v) -> v+1);
        hashMap.computeIfPresent("key1", (k, v) -> v+1);
    }

}
